<?php
/**
 * Description: 申请提现
 * User: devforma
 * Date: 16/3/2
 * Time: 16:53
 */

class ApplyWithdraw implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb, $uddb;

        if (validateUser($queryArgs) !== 1 || empty($queryArgs['withdraw_fee'])
            || !filter_var($queryArgs['withdraw_fee'], FILTER_VALIDATE_INT)) {
            return genOutput(1000);
        }

        $user = $uddb->getRow('SELECT `alipay_id`, `alipay_name`, `cashback_fee` FROM `user_social_info` WHERE `user_id`=:uid',
            array(':uid' => $queryArgs['user_id']));

        if (empty($user->alipay_id) || empty($user->alipay_name)) {
            return genOutput(8005);
        }

        if (empty($user->cashback_fee) || $user->cashback_fee < $queryArgs['withdraw_fee']) {
            return genOutput(8004);
        }

        $cmsdb->insert('withdraw_records', array(
            'user_id' => $queryArgs['user_id'],
            'total_fee' => $queryArgs['withdraw_fee'],
            'status' => 2,
            'alipay_account' => $user->alipay_id,
            'alipay_name' => $user->alipay_name,
            'create_time' => date('Y-m-d H:i:s')
        ));

        $cmsdb->update('user_social_info', array('cashback_fee' => $user->cashback_fee - $queryArgs['withdraw_fee']),
            array('user_id' => $queryArgs['user_id']));

        return genOutput(1);
    }
}